package com.zenkeeuk.bookerfornfc.client.android.book;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zenkeeuk.bookerfornfc.client.android.Contents;
import com.zenkeeuk.bookerfornfc.client.android.R;

import android.content.Context;
import android.widget.SimpleAdapter;

public class BookCompareAdapter extends SimpleAdapter {

    private static List<Map<String, Object>> reviews;

    public BookCompareAdapter(Context context, List<DoubanBookInfo> data) {
    	super(context, getData(data), 
    			R.layout.comparison, 
    			new String[]{Contents.BOOK_COMPARE_TITLE_KEY, 
    						 Contents.BOOK_COMPARE_RATING_KEY, 
    						 Contents.BOOK_COMPARE_IMG_KEY,
    						 Contents.BOOK_COMPARE_DATE_KEY}, 
    			new int[]{R.id.compare_title, 
    					  R.id.compare_rating, 
    					  R.id.compare_img,
    					  R.id.compare_date});
    }
    
    private static List<Map<String, Object>> getData(List<DoubanBookInfo> data) {
    	reviews = new ArrayList<Map<String, Object>>();
    	
    	for (int i=0;i<data.size();i++) {
	    	HashMap<String, Object> map = new HashMap<String, Object>();
	    	map.put(Contents.BOOK_COMPARE_TITLE_KEY, data.get(i).getBookName());
	    	
	    	if (data.get(i).getRating() != 0) {
            	map.put(Contents.BOOK_COMPARE_RATING_KEY, data.get(i).getRating());
            } else {
            	map.put(Contents.BOOK_COMPARE_RATING_KEY, Contents.DOUBAN_NO_RATING);
            }
	    	
	    	map.put(Contents.BOOK_COMPARE_IMG_KEY, Contents.DOUBAN_IMG_FOLDER_PATH + data.get(i).getBookisbn() + ".jpeg");
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	        Date mDate = data.get(i).getScanDate();
	        String date = sdf.format(mDate);
	    	map.put(Contents.BOOK_COMPARE_DATE_KEY, date);
	    	reviews.add(map);
    	}
    	
    	return reviews;
    }
}
